//package com.fingard.dsp.bank.directbank.citi01;
//
//import com.fingard.dsp.bank.directbank.bankInterface.IBatPayRec;
//import com.fingard.dsp.bank.reqrespobj.batpayrec.ReqBatPayRec;
//import com.fingard.dsp.bank.reqrespobj.batpayrec.ReqQryBatPayRec;
//import com.fingard.dsp.bank.reqrespobj.batpayrec.RetAtsBatPayRec;
//import com.fingard.dsp.bank.reqrespobj.batpayrec.RetAtsQryBatPayRec;
//import com.fingard.dsp.bank.reqrespobj.batpayrec.detail.RetAtsBatPayRecDetail;
//import com.fingard.text.StringHelper;
//import com.fingard.util.FtpUtil;
//import com.fingard.xml.XElement;
//import com.fingard.xml.XmlTextReader;
//import com.fingard.xml.XmlTextWriter;
//
//import java.io.File;
//
//import static com.fingard.io.FileHelper.readToString;
//import static com.fingard.text.StringHelper.trimEndFileSp;
//
//public class CITI01BatPayRec extends CITI01Base implements IBatPayRec {
//
//    @Override
//    public RetAtsBatPayRec d_1988_BatPay(ReqBatPayRec p_reqBat) throws Exception {
//        //花旗银行外币发工资走SFTP模式
//        // 返回ATS的对象
//        RetAtsBatPayRec tmpRetAtsDetail = new RetAtsBatPayRec(p_reqBat.reqHeader);
//        XmlTextWriter xmlWriter = new XmlTextWriter();
//        writeCommonHeader(xmlWriter);
//        if(p_reqBat.reqHeader.commandCode.equals("1988101")){
//            //代发报文
//            writeBatFileContent1988101(xmlWriter,p_reqBat);
//        }else{
//            //批量付款
//            writeBatFileContent1988102(xmlWriter,p_reqBat);
//        }
//        String filefullname = getBankConfig().getValueAsString("localAddress") + p_reqBat.payDate + "_" + p_reqBat.reqBatchNO;
//        WriteBankLogLn("接受到付款文件报文为：" + xmlWriter.toXmlString());
//        if (!exportContentToFile(xmlWriter.toXmlString(), filefullname)) {
//            tmpRetAtsDetail.respCode = "0";
//            tmpRetAtsDetail.respInfo = "生成付款文件失败";
//            tmpRetAtsDetail.transState = "E1301";
//            tmpRetAtsDetail.payInfoCode = "E1301";
//            tmpRetAtsDetail.reqSeqID = p_reqBat.reqBatchNO;
//            return tmpRetAtsDetail;
//        }
//        if (!encrpyFile(filefullname)) {
//            tmpRetAtsDetail.respCode = "0";
//            tmpRetAtsDetail.respInfo = "生成加密文件失败";
//            tmpRetAtsDetail.transState = "E1301";
//            tmpRetAtsDetail.payInfoCode = "E1301";
//            tmpRetAtsDetail.reqSeqID = p_reqBat.reqBatchNO;
//            return tmpRetAtsDetail;
//        }
//        File gpgFile = new File(filefullname + ".gpg");
//        String filename = getBankConfig().getValueAsString("fileNameType") + p_reqBat.reqBatchNO;
//        String content = readToString(gpgFile, "ascii");
//        WriteBankLogLn("生成的加密付款文件内容为：" + content);
//        if (!uploadFile(filename, content)) {
//            tmpRetAtsDetail.respCode = "0";
//            tmpRetAtsDetail.respInfo = "上送文件至SFTP服务器失败";
//            tmpRetAtsDetail.transState = "E1301";
//            tmpRetAtsDetail.payInfoCode = "E1301";
//            tmpRetAtsDetail.reqSeqID = p_reqBat.reqBatchNO;
//            return tmpRetAtsDetail;
//        }
//
//        tmpRetAtsDetail.respCode = "0";
//        tmpRetAtsDetail.respInfo = "发送文件成功";
//        tmpRetAtsDetail.transState = "0000";
//        tmpRetAtsDetail.payInfoCode = "0000";
//        tmpRetAtsDetail.reqSeqID = p_reqBat.reqBatchNO;
//        tmpRetAtsDetail.writeRetToATS();
//        return tmpRetAtsDetail;
//    }
//
//    @Override
//    public RetAtsQryBatPayRec d_1998_QryBatPay(ReqQryBatPayRec p_req1998) throws Exception {
//        // 返回ATS的对象
//        RetAtsQryBatPayRec retAts = new RetAtsQryBatPayRec(p_req1998.reqHeader);
//        try {
//            //解析对应的文件
//            String batStatusFilePath = trimEndFileSp(getBankConfig().getValueAsString("BATFilePath"));
//            File mtFiles =  new File(batStatusFilePath);
//            if(mtFiles.listFiles()!=null){
//                //遍历所有的ACK文件
//                for(File file:mtFiles.listFiles()) {
//                    if (file.getName().equals(p_req1998.reqBatchNO)) {
//                        String ackContent = FtpUtil.getTextFromFile(batStatusFilePath + File.separator + file.getName(), getCharset());
//                        StringBuilder tmpSbMsg = new StringBuilder();
//                        XmlTextReader tmpBkReader = loadBankRespXmlReader(ackContent, tmpSbMsg);
//                        if (tmpBkReader != null) {
//                            String tmpRetCode = tmpBkReader.getFirstText("/Document/CstmrPmtStsRpt/OrgnlGrpInfAndSts/NbOfTxsPerSts/DtldSts");
//                            String tmpRetMsg = "";
//                            if (StringHelper.hasAnyChar(tmpRetCode)) {
//                                int flag = checkStatus(tmpRetCode);
//                                if (flag == 3 || flag == 1) {
//                                    retAts.respCode = "0";
//                                    retAts.reqSeqID = p_req1998.reqBatchNO;
//                                    retAts.writeBeforeDetail();
//                                    while (tmpBkReader.read("/Document/CstmrPmtStsRpt/OrgnlPmtInfAndSts")) { {
//                                            XElement tmpRetNode = tmpBkReader.readedXElement;
//                                            RetAtsBatPayRecDetail tmpRetDetail = new RetAtsBatPayRecDetail();
//                                            tmpRetDetail.payAct = tmpRetNode.getSingleText("TxInfAndSts/OrgnlTxRef/DbtrAcct/Id/Othr/Id");
//                                            tmpRetDetail.payName = "";
//                                            tmpRetDetail.recName = "";
//                                            tmpRetDetail.recAct = tmpRetNode.getSingleText("TxInfAndSts/OrgnlTxRef/CdtrAcct/Id/Othr/Id");
//                                            tmpRetDetail.payAmount = tmpRetNode.getSingleText("TxInfAndSts/OrgnlTxRef/Amt/InstdAmt");
//                                            //读取币种
//                                            String ccyStr = tmpRetNode.getSingleText("TxInfAndSts/OrgnlTxRef/Amt");
//                                            String[] ccy = ccyStr.split("\"");
//                                            tmpRetDetail.curType = convertCurToATS(ccy[1]);
//                                            tmpRetDetail.result =  "1998+" + tmpRetNode.getSingleText("TxInfAndSts/TxSts");
//                                            tmpRetDetail.payInfoCode = tmpRetNode.getSingleText("TxInfAndSts/TxSts");
//                                            tmpRetDetail.postScript = "";
//                                            tmpRetDetail.memo = "";
//                                            // tmpRetDetail.usage = strings[1];
//                                            retAts.writeRetDetail(tmpRetDetail);
//                                        }
//                                    }
//                                }  else if(flag == 2){
//                                    retAts.respCode = "-1";
//                                    retAts.respInfo = tmpRetCode;
//                                } else {
//                                    retAts.respCode = "-1";
//                                    retAts.respInfo = "返回的交易状态未知";
//                                }
//                            } else {
//                                retAts.respCode = "-1";
//                                retAts.respInfo = "返回的交易状态码为空";
//                            }
//                        } else {
//                            retAts.respCode = "-1";
//                            retAts.respInfo = tmpSbMsg.toString();
//                        }
//                    } else {
//                        retAts.respCode = "-1";
//                        retAts.respInfo = "查询请求失败";
//                    }
//                }
//            }
//        } catch (Exception e) {
//            retAts.respCode = "-1";
//            retAts.respInfo = e.getMessage();
//            e.printStackTrace();
//        }
//        if ("-1".equals(retAts.respCode) && StringHelper.isNullOrEmpty(retAts.reqSeqID)) {
//            retAts.reqSeqID = p_req1998.reqBatchNO;
//        }
//        if ("-1".equals(retAts.respCode)) {
//            retAts.writeBeforeDetail();
//        }
//        retAts.writeAfterDetail();
//        return retAts;
//    }
//
//    @Override
//    public RetAtsBatPayRec d_9188_BatPay(ReqBatPayRec p_req9188) throws Exception {
//        return null;
//    }
//
//    @Override
//    public RetAtsQryBatPayRec d_9198_QryBatPay(ReqQryBatPayRec p_req9198) throws Exception {
//        return null;
//    }
//
//    private void writeBatFileContent1988101(XmlTextWriter xmlWriter, ReqBatPayRec reqForeignCurrSingle) throws Exception {
//        int i = 0;
//        xmlWriter.writeStartElement("GrpHdr");
//        xmlWriter.writeElementStringNotNull("MsgId", reqForeignCurrSingle.reqBatchNO);
//
//        String date = reqForeignCurrSingle.payDate+ "T" + reqForeignCurrSingle.payTime;
//        xmlWriter.writeElementStringNotNull("CreDtTm", date);
//        xmlWriter.writeElementStringNotNull("NbOfTxs", reqForeignCurrSingle.totalNum);
//        xmlWriter.writeStartElement("InitgPty");
//        String nm = getBankConfig().getMapValueByKey("actNameMappingBat", reqForeignCurrSingle.ownAct);
//        xmlWriter.writeElementStringNotNull("Nm", nm);
//        xmlWriter.writeEndElement();
//        xmlWriter.writeEndElement();
//        xmlWriter.writeStartElement("PmtInf");
//        //流水号
//        xmlWriter.writeElementStringNotNull("PmtInfId", reqForeignCurrSingle.reqBatchNO);
//        xmlWriter.writeElementStringNotNull("PmtMtd", "TRF");
//        xmlWriter.writeStartElement("PmtTpInf");
//        xmlWriter.writeStartElement("SvcLvl");
//        xmlWriter.writeElementStringNotNull("Cd", "NURG");
//        xmlWriter.writeEndElement();
//        xmlWriter.writeStartElement("CtgyPurp");
//        xmlWriter.writeElementStringNotNull("Cd", "SALA");
//        xmlWriter.writeEndElement();
//        xmlWriter.writeEndElement();
//        xmlWriter.writeElementStringNotNull("ReqdExctnDt", reqForeignCurrSingle.payDate);
//        xmlWriter.writeStartElement("Dbtr");
//        xmlWriter.writeElementStringNotNull("Nm", nm);
//        xmlWriter.writeEndElement();
//        xmlWriter.writeStartElement("DbtrAcct");
//        xmlWriter.writeStartElement("Id");
//        xmlWriter.writeStartElement("Othr");
//        xmlWriter.writeElementStringNotNull("Id", reqForeignCurrSingle.ownAct);
//        xmlWriter.writeEndElement();
//        xmlWriter.writeEndElement();
//        xmlWriter.writeEndElement();
//        xmlWriter.writeStartElement("DbtrAgt");
//        xmlWriter.writeStartElement("FinInstnId");
//        xmlWriter.writeElementStringNotNull("BIC", reqForeignCurrSingle.ownSwiftCode); //reqForeignCurrSingle.paySWIFTCODE
//        xmlWriter.writeStartElement("PstlAdr");
//        xmlWriter.writeElementStringNotNull("Ctry", reqForeignCurrSingle.ownArea);
//        xmlWriter.writeEndElement();
//        xmlWriter.writeEndElement();
//        xmlWriter.writeEndElement();
//        while(reqForeignCurrSingle.readRecord()){
//            xmlWriter.writeStartElement("CdtTrfTxInf");
//            xmlWriter.writeStartElement("PmtId");
//            xmlWriter.writeElementStringNotNull("EndToEndId", reqForeignCurrSingle.readedDetail.askNumber);
//            xmlWriter.writeEndElement();
//            xmlWriter.writeStartElement("Amt");
//            String currency = convertCurToBank(reqForeignCurrSingle.readedDetail.payCur);
//            xmlWriter.writeElementAttribute("InstdAmt",reqForeignCurrSingle.readedDetail.payAmount,"Ccy",currency);
//            xmlWriter.writeEndElement();
//            xmlWriter.writeElementStringNotNull("ChrgBr", FeeBearer(reqForeignCurrSingle.readedDetail.costMode));//费用承担字段 reqForeignCurrSingle.readedDetail.costMode
//            xmlWriter.writeStartElement("CdtrAgt");
//            xmlWriter.writeStartElement("FinInstnId");
//            xmlWriter.writeStartElement("ClrSysMmbId");
//
//            xmlWriter.writeElementStringNotNull("MmbId", reqForeignCurrSingle.readedDetail.recBankCode);
//            xmlWriter.writeEndElement();
//            xmlWriter.writeEndElement();
//
//
//            //     xmlWriter.writeElementStringNotNull("BIC", reqForeignCurrSingle.readedDetail.recSwiftCode);
//     //       xmlWriter.writeElementStringNotNull("Nm", reqForeignCurrSingle.readedDetail.recBankName);
//      //      xmlWriter.writeStartElement("PstlAdr");
//       //     xmlWriter.writeElementStringNotNull("AdrTp", "ADDR");
//       //     xmlWriter.writeElementStringNotNull("AdrLine", "TEST");
//       //     xmlWriter.writeEndElement();
//       //     xmlWriter.writeEndElement();
//            xmlWriter.writeEndElement();
//            xmlWriter.writeStartElement("Cdtr");
//            xmlWriter.writeElementStringNotNull("Nm", reqForeignCurrSingle.readedDetail.recName);
//            xmlWriter.writeStartElement("PstlAdr");
//            xmlWriter.writeElementStringNotNull("AdrTp", reqForeignCurrSingle.readedDetail.recNameAddress);
//            xmlWriter.writeElementStringNotNull("AdrLine", "PRO");
//            xmlWriter.writeEndElement();
//            xmlWriter.writeEndElement();
//            xmlWriter.writeStartElement("CdtrAcct");
//            xmlWriter.writeStartElement("Id");
//            xmlWriter.writeStartElement("Othr");
//            xmlWriter.writeElementStringNotNull("Id", reqForeignCurrSingle.readedDetail.recAct);
//            xmlWriter.writeEndElement();
//            xmlWriter.writeEndElement();
//            xmlWriter.writeEndElement();
//          //  xmlWriter.writeStartElement("RmtInf");
//          //  xmlWriter.writeElementStringNotNull("Ustrd", reqForeignCurrSingle.readedDetail.transInfo);
//          //  xmlWriter.writeEndElement();
//            xmlWriter.writeEndElement();
//        }
//        xmlWriter.writeEndElement();
//        xmlWriter.writeEndElement();
//        xmlWriter.writeStartDocByDeclaration("\r\n"+"</Document>");
//    }
//
//    private void writeBatFileContent1988102(XmlTextWriter xmlWriter, ReqBatPayRec reqForeignCurrSingle) throws Exception {
//        int i = 0;
//        xmlWriter.writeStartElement("GrpHdr");
//        xmlWriter.writeElementStringNotNull("MsgId", reqForeignCurrSingle.reqBatchNO);
//
//        String date = reqForeignCurrSingle.payDate+ "T" + reqForeignCurrSingle.payTime;
//        xmlWriter.writeElementStringNotNull("CreDtTm", date);
//        xmlWriter.writeElementStringNotNull("NbOfTxs", reqForeignCurrSingle.totalNum);
//        xmlWriter.writeStartElement("InitgPty");
//        xmlWriter.writeElementStringNotNull("Nm", reqForeignCurrSingle.ownActName);
//        xmlWriter.writeEndElement();
//        xmlWriter.writeEndElement();
//        xmlWriter.writeStartElement("PmtInf");
//        //流水号
//        xmlWriter.writeElementStringNotNull("PmtInfId", reqForeignCurrSingle.reqBatchNO+"1");
//        xmlWriter.writeElementStringNotNull("PmtMtd", "TRF");
//        xmlWriter.writeStartElement("PmtTpInf");
//        xmlWriter.writeStartElement("SvcLvl");
//        xmlWriter.writeElementStringNotNull("Cd", "NURG");
//        xmlWriter.writeEndElement();
//        xmlWriter.writeEndElement();
//        // xmlWriter.writeStartElement("CtgyPurp");
//      //  xmlWriter.writeElementStringNotNull("Cd", "SALA");
//      //        xmlWriter.writeEndElement();
//        xmlWriter.writeElementStringNotNull("ReqdExctnDt", reqForeignCurrSingle.payDate);
//        xmlWriter.writeStartElement("Dbtr");
//        xmlWriter.writeElementStringNotNull("Nm", reqForeignCurrSingle.ownActName);
//        xmlWriter.writeEndElement();
//        xmlWriter.writeStartElement("DbtrAcct");
//        xmlWriter.writeStartElement("Id");
//        xmlWriter.writeStartElement("Othr");
//        xmlWriter.writeElementStringNotNull("Id", reqForeignCurrSingle.ownAct);
//        xmlWriter.writeEndElement();
//        xmlWriter.writeEndElement();
//        xmlWriter.writeEndElement();
//        xmlWriter.writeStartElement("DbtrAgt");
//        xmlWriter.writeStartElement("FinInstnId");
//        xmlWriter.writeElementStringNotNull("BIC", reqForeignCurrSingle.ownSwiftCode); //reqForeignCurrSingle.paySWIFTCODE
//        xmlWriter.writeStartElement("PstlAdr");
//        xmlWriter.writeElementStringNotNull("Ctry", reqForeignCurrSingle.ownArea);
//        xmlWriter.writeEndElement();
//        xmlWriter.writeEndElement();
//        xmlWriter.writeEndElement();
//        while(reqForeignCurrSingle.readRecord()){
//            xmlWriter.writeStartElement("CdtTrfTxInf");
//            xmlWriter.writeStartElement("PmtId");
//            xmlWriter.writeElementStringNotNull("EndToEndId", reqForeignCurrSingle.readedDetail.askNumber);
//            xmlWriter.writeEndElement();
//            xmlWriter.writeStartElement("Amt");
//            String currency = convertCurToBank(reqForeignCurrSingle.readedDetail.payCur);
//            xmlWriter.writeElementAttribute("InstdAmt",reqForeignCurrSingle.readedDetail.payAmount,"Ccy",currency);
//            xmlWriter.writeEndElement();
//            xmlWriter.writeElementStringNotNull("ChrgBr", FeeBearer(reqForeignCurrSingle.readedDetail.costMode));//费用承担字段 reqForeignCurrSingle.readedDetail.costMode
//            xmlWriter.writeStartElement("CdtrAgt");
//            xmlWriter.writeStartElement("FinInstnId");
//            xmlWriter.writeStartElement("ClrSysMmbId");
//
//            xmlWriter.writeElementStringNotNull("MmbId", reqForeignCurrSingle.readedDetail.recBankCode);
//            xmlWriter.writeEndElement();
//            xmlWriter.writeEndElement();
//            xmlWriter.writeEndElement();
//            xmlWriter.writeStartElement("Cdtr");
//            xmlWriter.writeElementStringNotNull("Nm", reqForeignCurrSingle.readedDetail.recName);
//            xmlWriter.writeStartElement("PstlAdr");
//            xmlWriter.writeElementStringNotNull("AdrTp", "ADDR");
//            xmlWriter.writeElementStringNotNull("AdrLine", "PRO");
//            xmlWriter.writeEndElement();
//            xmlWriter.writeEndElement();
//            xmlWriter.writeStartElement("CdtrAcct");
//            xmlWriter.writeStartElement("Id");
//            xmlWriter.writeStartElement("Othr");
//            xmlWriter.writeElementStringNotNull("Id", reqForeignCurrSingle.readedDetail.recAct);
//            xmlWriter.writeEndElement();
//            xmlWriter.writeEndElement();
//            xmlWriter.writeEndElement();
//            //  xmlWriter.writeStartElement("RmtInf");
//            //  xmlWriter.writeElementStringNotNull("Ustrd", reqForeignCurrSingle.readedDetail.transInfo);
//            //  xmlWriter.writeEndElement();
//            xmlWriter.writeEndElement();
//        }
//        xmlWriter.writeEndElement();
//        xmlWriter.writeEndElement();
//        xmlWriter.writeStartDocByDeclaration("\r\n"+"</Document>");
//    }
//}
